import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/automounting_provider.dart';
import 'package:xiongan_app_yy/provider/into_warehousing_provider.dart';
import 'package:xiongan_app_yy/widgets/comm/CustomDialog.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

class IntoHandoverDetails extends StatefulWidget {
  dynamic data;

  IntoHandoverDetails(this.data);

  @override
  _IntoHandoverDetailsState createState() => _IntoHandoverDetailsState();
}

class _IntoHandoverDetailsState extends State<IntoHandoverDetails> {
  bool load = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "入库交接",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
          visibility: load,
          bgColor: Colors.black26,
          builder: Column(
            children: <Widget>[
              Container(
                color: Colors.white,
                padding: EdgeInsets.all(10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    RichText(
                      text: TextSpan(
                        text: "入库任务单号：",
                        style:
                            TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                        children: <TextSpan>[
                          TextSpan(
                            text: "${widget.data["orderno"]}",
                            style: TextStyle(
                                fontSize: 14.0, color: Color(0xFF00A5F1)),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(bottom: 6, top: 6),
                      child: _Text("项目名称: ${widget.data["projectname"]}"),
                    ),
                    _Text("清点人：${widget.data["auditorname"]}"),
                    Padding(
                      padding: EdgeInsets.only(bottom: 6, top: 6),
                      child: _Text("入库物资种数: ${widget.data["skucount"]}"),
                    ),
                    _Text("入库物资总数：${widget.data["materialcount"]}"),
                    GestureDetector(
                      child: Container(
                        padding: EdgeInsets.only(top: 8, bottom: 2),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                              child: _Text("物资明细"),
                            ),
                            Center(
                              child: Container(
                                width: 8,
                                height: 16,
                                child: Image.asset("assets/images/arrow.png"),
                              ),
                            )
                          ],
                        ),
                      ),
                      onTap: () {
                        Navigator.pushNamed(
                            context, '/into_handover_details_info',
                            arguments: widget.data["id"]);
                      },
                    )
                  ],
                ),
              ),
              Expanded(child: Container()),
              Container(
                  margin: EdgeInsets.only(bottom: 10),
                  child: Center(
                    child: FlatButton(
                        color: Colors.blue,
                        onPressed: () {
                          submitOrder(widget.data['id']);
                        },
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(6)),
                        child: Text(
                          "确认交接",
                          style: TextStyle(color: Colors.white),
                        )),
                  ))
            ],
          ),
        ));
  }

  void submitOrder(String id) {
    setState(() {
      load = true;
    });
    Provider.of<IntoWareHousingProvider>(context).sendOrder(id).then((a) {
      setState(() {
        load = false;
      });
      if (a == true) {
        showDialog(
            context: context,
            barrierDismissible: true,
            builder: (context) {
              return CustomDialog(
                title: "交接成功",
                content: "是否去上架",
                outsideDismiss: false,
                confirmCallback: () {
                  Provider.of<AutoMountingProvider>(context)
                      .getDetailListData(orderid: widget.data['id']);
                  Navigator.popAndPushNamed(context, '/auto_mounting_type');
                },
                dismissCallback: () {
                  Provider.of<IntoWareHousingProvider>(context).getList();
                  Navigator.pop(context);
                },
              );
            });
      }
    });
  }
}

class _Text extends Text {
  TextStyle style;

  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}
